package com.foreknow.example;

import java.util.Scanner;

/**
 * 测试类 i. 输出图书的信息 ii. 用户购买图书 iii. 输出订单的信息
 * 
 * @author ttc
 *
 */
public class Test {
	// i. 输出图书的信息
	public void outBooks(Book[] books) {
		// 输出Book对象的信息
		for (int i = 0; i < books.length; i++) {
			Book book = books[i];
			System.out.println(
					book.getPid() + "   " + book.getPname() + "   " + book.getPrice() + "   " + book.getStroage());
		}
	}
	
	//ii. 用户购买图书
	public Order buy(Book[] books) {
		Scanner input = new Scanner(System.in);
		//下一个订单
		Order order = new Order("00001");
		//循环3次购买图书信息
		for(int i = 0;i<3;i++){
			System.out.println("请输入图书编号选择图书：");
			int cno = input.nextInt();
			System.out.println("请输入购买图书数量：");
			int pnum = input.nextInt();
			//创建一个订单项对象
			OrderItem item = new OrderItem(books[cno-1].getPname(), books[cno-1].getPrice(), pnum);
			//将以上的OrderItem对象添加到OrderItem[] items;//订单项列表中
			order.setItems(item, i);
			System.out.println("请继续购买图书");
		}	
		return order;
	}
	
	//iii. 输出订单的信息
	public void showOrderInfo(Order order) {
		//1.输出订单编号
		System.out.println("订单编号为"+order.getOrderId());
		//2.如何获取到Order中的数组列表OrderItem[] items;//订单项列表
		OrderItem[] items = order.getItems();
		System.out.println("----------------------------------------");
		for(int i = 0;i<items.length;i++){
			OrderItem oItem =  items[i];
			System.out.println(oItem.getPname()+"--"+oItem.getNum()+"--"+oItem.getPrice());
		}
		System.out.println("----------------------------------------");
		System.out.println("订单的总额为："+order.getTotal());
	}

	public static void main(String[] args) {
		//创建一个Book类型的数组
		Book[] books = new Book[3];
		//创建三个Book对象并将这些对象保存到数组中
		Book book1 = new Book(1, "Java教程", 30.6, 30);
		Book book2 = new Book(2, "JSP 指南", 42.1, 40);
		Book book3 = new Book(3, "SSH 架构", 50, 15);
		books[0] = book1;
		books[1] = book2;
		books[2] = book3;
		
		Test test = new Test();
		//调用方法
		test.outBooks(books);
		
		//调用购买图书的方法
		Order order = test.buy(books);
		
		//调用输出订单的方法
		test.showOrderInfo(order);
	}
}
